object work1 {
  def main(args: Array[String]): Unit = {
    // Book类
    case class Book(name: String, author: String, isbn: String, var isBorrowed: Boolean = false) {
      def borrow(): Unit = {
        if (isBorrowed) {
          println("这本书已经被借出了！")
        } else {
          isBorrowed = true
          println("借阅成功！")
        }
      }

      def returnBook(): Unit = {
        if (isBorrowed) {
          isBorrowed = false
          println("归还成功！")
        } else {
          println("这本书没有被借出，不需要归还！")
        }
      }
    }

    // Library类
    class Library(var availableBooks: List[Book], var borrowedBooks: List[Book]) {
      def addBook(book: Book): Unit = {
        availableBooks = book :: availableBooks
        println("成功添加新书：" + book.name)
      }

      def borrowBook(isbn: String): Unit = {
        availableBooks.find(book => book.isbn == isbn) match {
          case Some(book) =>
            availableBooks = availableBooks.filterNot(_ == book)
            book.borrow()
            borrowedBooks = book :: borrowedBooks
          case None =>
            println("没有找到该书号的书！")
        }
      }

      def returnBook(isbn: String): Unit = {
        borrowedBooks.find(book => book.isbn == isbn) match {
          case Some(book) =>
            borrowedBooks = borrowedBooks.filterNot(_ == book)
            book.returnBook()
            availableBooks = book :: availableBooks
          case None =>
            println("没有找到该书号的书！")
        }
      }
    }
        val book1 = Book("Scala编程", "张三", "123")
        val book2 = Book("Java程序设计", "李四", "111")
        val book3 = Book("Python核心编程", "王五", "222")

        val library = new Library(List(book1, book2), List())

        // 添加新书
        library.addBook(book3)

        // 借出书籍
        library.borrowBook("123") // 已经借出了
        library.borrowBook("456") // 没有找到该书号的书

        // 归还书籍
        library.returnBook("123") // 这本书没有被借出，不需要归还
        library.returnBook("456") // 没有找到该书号的书
  }
}

